<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="/WEB-INF/tlds/struts-html.tld"  prefix="html" %>
<%@ taglib uri="/WEB-INF/tlds/struts-bean.tld"  prefix="bean" %>
<%@ taglib uri="/WEB-INF/tlds/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/tlds/custom-html.tld" prefix="chtml" %>

<%@ page  import="java.util.*" %>
<%@ page  import="com.erp.framework.util.CommonUtil" %>
<%@ page  import="com.erp.app.common.ProjectConstants" %>
<%@ page  import="org.apache.commons.beanutils.*" %>
<%@ page  import="com.erp.common.web.*" %>

<jsp:useBean id="searchForm" scope="session" type="com.erp.common.web.SearchForm" />

<%
ArrayList searchResultList = searchForm.getMultiRows();
pageContext.setAttribute("searchResultList", searchResultList);

HashMap params = searchForm.getParams();
int totalDisplayRow = 22;

Collection autoFillList = (ArrayList)params.get(ProjectConstants.SESSION_SEARCH_CRITERIA_AUTOFILL_LIST);
%>

<head>
<title>Search</title>
<link rel="stylesheet" href="<%=request.getContextPath()+ "/css/style.css" %>" type="text/css">

<script language="javascript">
<!--
var isSubmitted = 0;
 
function doSubmit(event) {
	document.forms[0].pageEvent.value = event;
	isSubmitted = 1;
	document.forms[0].submit();
}

function doSelect(idx) {
<% if (searchForm.getIsExclusive()) { %>
	var isDelete = document.forms[0].isDelete;
	for (i = 0; i < isDelete.length; i++ ){
		if (isDelete[i].checked && i != idx) {
			document.forms[0].isDelete[i].checked = false;
		}
	}
<% } %>
}

function doLoad() {
	<% 
	if (!CommonUtil.isEmpty(searchForm.getPageEvent())) {
		if (searchForm.getPageEvent().equals("ok")) {
			out.println("window.opener.doSubmit('doNothing');");
			out.println("self.close();");
		}
	}
	%>
	<% if (searchForm.getNeedResize() != null && searchForm.getNeedResize().equals(ProjectConstants.GENERIC_YES)) { %>
	<% } else { %> 
		window.resizeTo(<%=searchForm.getResizeToX()%>, <%=searchForm.getResizeToY()%>);
	<% } %>
	window.moveTo(0, 0);
}

	function autofill(idx) {
		var from = document.getElementsByName("field" + idx);
		var to = document.getElementsByName("fieldTo" + idx);
		
		if (to.length <= 0)
			return;	// shouldn't happen
			
		to[0].value = to[0].value.length == 0 ? from[0].value : to[0].value;

		var tr = to[0].createTextRange();
		tr.select();

	}
	
		function formReset(){
    		var frm=document.forms.searchForm;    		 
    		for (i=0; i<frm.elements.length; i++){   	
    			if(frm.elements[i].name.substring(0,5)=="field"){
					frm.elements[i].value = "";
				}
			}
		}
-->
</script>
</head>
<body onLoad="javascript:doLoad();" bgcolor="#FFFFFF">

<html:form action="/search">
<html:hidden property="pageEvent" />
<html:hidden property="pageMode" />
<html:hidden property="needResize" />

<% 
LinkedList criteriaList = (LinkedList) params.get(ProjectConstants.SESSION_CRITERIA_LIST);
if(criteriaList != null && criteriaList.size() > 0){
	Iterator iterator = criteriaList.iterator();
	int count = 0; %>
	
<table class="table_crit">

<% if(searchForm.isShowFromTo()){ %>
<tr valign="top">
	<td class="col_label" width="100">&nbsp;</td>
	<td class="col_label">From</td>
	<td class="col_label">To</td>
</tr>	
<% } %>

<%	while (iterator.hasNext()) {
		String fieldText = "";
		String fieldType = "";
		String fieldValue = "";

		if(iterator.hasNext()){
			fieldText = (String) iterator.next();
		}

		if(iterator.hasNext()){
			fieldType = (String) iterator.next();
		}

		if(iterator.hasNext()){
			fieldValue = (String) iterator.next();
		}

		String table = "";
		String valCol = "";
		String textCol = "";
		String whereClause = "";
		String groupBy = "";
		String orderBy = "";
		String changeAction = "";

		if (fieldType == null){
			continue;
		}
		count++;
	%>
		<% if (fieldType.equals("HIDDEN")) { %>
			<input type="hidden" name="field<%=count%>" value="<%=CommonUtil.nullToString(fieldValue)%>">
			<input type="hidden" name="fieldTo<%=count%>" value="<%=CommonUtil.nullToString(fieldValue)%>">
		<% } else {%>
			<tr valign="top">
				<td class="col_label"><%=fieldText%>:</td>
				<% if (fieldType.equals("DISPLAY")) { %>
					<td class="col_value" colspan=2>
						<%=fieldValue%>
						<input type="hidden" name="field<%=count%>" value="<%=CommonUtil.nullToString(fieldValue)%>">
						<input type="hidden" name="fieldTo<%=count%>" value="<%=CommonUtil.nullToString(fieldValue)%>">
					</td>
				<% } %>

				<% if (fieldType.equals("LABEL")) { %>
					<td class="col_value" colspan=2>
						<%=fieldValue%>
					</td>
				<% } %>

				<% if (fieldType.equals("INPUT")) { %>
					<td class="col_value">
						<input type="text" name="field<%=count%>" value="<%=CommonUtil.nullToString(searchForm.getFormFieldVal(count))%>" size=30 class="t2" <% if(autoFillList != null && (autoFillList.contains("A") || autoFillList.contains(String.valueOf(count)) )) { %>onblur="javascript:autofill(<%=count%>)" <% } %>>
					</td>
					<td class="col_value">
						<input type="text" name="fieldTo<%=count%>" value="<%=CommonUtil.nullToString(searchForm.getFormFieldToVal(count))%>" size=30 class="t2" >
					</td>
				<% } %>
				
				<% if (fieldType.equals("DATE")) { %>
					<td class="col_value">
						<input type="text" name="field<%=count%>" value="<%=CommonUtil.nullToString(searchForm.getFormFieldVal(count))%>" size=30 class="t2" <% if(autoFillList != null && (autoFillList.contains("A") || autoFillList.contains(String.valueOf(count)) )) { %> <% } %>>
					</td>
					<td class="col_value">
						<input type="text" name="fieldTo<%=count%>" value="<%=CommonUtil.nullToString(searchForm.getFormFieldToVal(count))%>" size=30 class="t2" >
					</td>
				<% } %>

				<% if (fieldType.equals("COMBO")) { %>
					<%
						if(iterator.hasNext()){
							table = (String) iterator.next();
						}
						if(iterator.hasNext()){
							valCol = (String) iterator.next();
						}
						if(iterator.hasNext()){
							textCol = (String) iterator.next();
						}
						if(iterator.hasNext()){
							whereClause = (String) iterator.next();
						}
						if(iterator.hasNext()){
							orderBy = (String) iterator.next();
						}
						if(iterator.hasNext()){
							changeAction = CommonUtil.nullToString((String) iterator.next());
						}

						whereClause = searchForm.replaceFilterVal(whereClause);
					%>
					<td class="col_value" colspan="2">
						<% if (!CommonUtil.isEmpty(changeAction)){%>
							<select name="field<%=count%>" class="t2" onchange="this.form.fieldTo<%=count%>.value=this.value; doSubmit(<%=changeAction%>);">
						<% } else { %>
							<select name="field<%=count%>" class="t2" onchange="this.form.fieldTo<%=count%>.value=this.value;">
						<% } %>

						<option value=""></option>
						<chtml:options table="<%=table%>"
									valuecolumn="<%=valCol%>"
									textcolumn="<%=textCol%>"
									where="<%=whereClause%>"
									order="<%=orderBy%>"
									selected="<%=searchForm.getFormFieldVal(count)%>" />
						</select>
						<input type="hidden" name="fieldTo<%=count%>" value="<%=searchForm.getFormFieldVal(count)%>">
					</td>
				<% } %>


				<% if (fieldType.equals("COMBO_WITH_GROUP_BY")) { %>
					<%
						if(iterator.hasNext()){
							table = (String) iterator.next();
						}
						if(iterator.hasNext()){
							valCol = (String) iterator.next();
						}
						if(iterator.hasNext()){
							textCol = (String) iterator.next();
						}
						if(iterator.hasNext()){
							whereClause = (String) iterator.next();
						}
						if(iterator.hasNext()){
							groupBy = (String) iterator.next();
						}
						if(iterator.hasNext()){
							orderBy = (String) iterator.next();
						}
						if(iterator.hasNext()){
							changeAction = CommonUtil.nullToString((String) iterator.next());
						}

						whereClause = searchForm.replaceFilterVal(whereClause);
					%>
					<td class="col_value" colspan="2">
						<% if (!CommonUtil.isEmpty(changeAction)){%>
							<select name="field<%=count%>" class="t2" onchange="this.form.fieldTo<%=count%>.value=this.value; doSubmit(<%=changeAction%>);">
						<% } else { %>
							<select name="field<%=count%>" class="t2" onchange="this.form.fieldTo<%=count%>.value=this.value;">
						<% } %>

						<option value=""></option>
						<chtml:options table="<%=table%>"
									valuecolumn="<%=valCol%>"
									textcolumn="<%=textCol%>"
									where="<%=whereClause%>"
									group="<%=groupBy%>"
									order="<%=orderBy%>"
									selected="<%=searchForm.getFormFieldVal(count)%>" />
						</select>
						<input type="hidden" name="fieldTo<%=count%>" value="<%=searchForm.getFormFieldVal(count)%>">
					</td>
				<% } %>

				




				<% if (fieldType.equals("COMBO_WITH_FROM_TO")) { %>
					<%
						if(iterator.hasNext()){
							table = (String) iterator.next();
						}
						if(iterator.hasNext()){
							valCol = (String) iterator.next();
						}
						if(iterator.hasNext()){
							textCol = (String) iterator.next();
						}
						if(iterator.hasNext()){
							whereClause = (String) iterator.next();
						}
						if(iterator.hasNext()){
							orderBy = (String) iterator.next();
						}
						if(iterator.hasNext()){
							changeAction = CommonUtil.nullToString((String) iterator.next());
						}

						whereClause = searchForm.replaceFilterVal(whereClause);
				%>
					<td class="col_value">
						<select name="field<%=count%>" class="t2" <% if (!CommonUtil.isEmpty(changeAction)){ out.println("onChange=\"doSubmit("+changeAction+");\"");} %>>
						<option value=""></option>
						<chtml:options table="<%=table%>"
									valuecolumn="<%=valCol%>"
									textcolumn="<%=textCol%>"
									where="<%=whereClause%>"
									order="<%=orderBy%>"
									selected="<%=CommonUtil.nullToString(searchForm.getFormFieldVal(count))%>" />
						</select>
					</td>
					<%
						if(iterator.hasNext()){
							table = (String) iterator.next();
						}
						if(iterator.hasNext()){
							valCol = (String) iterator.next();
						}
						if(iterator.hasNext()){
							textCol = (String) iterator.next();
						}
						if(iterator.hasNext()){
							whereClause = (String) iterator.next();
						}
						if(iterator.hasNext()){
							orderBy = (String) iterator.next();
						}
						if(iterator.hasNext()){
							changeAction = CommonUtil.nullToString((String) iterator.next());
						}

						whereClause = searchForm.replaceFilterToVal(whereClause);
					%>
					<td class="col_value">
						<select name="fieldTo<%=count%>" class="t2" <% if (!CommonUtil.isEmpty(changeAction)){ out.println("onChange=\"doSubmit("+changeAction+");\"");} %>>
						<option value=""></option>
						<chtml:options table="<%=table%>"
									valuecolumn="<%=valCol%>"
									textcolumn="<%=textCol%>"
									where="<%=whereClause%>"
									order="<%=orderBy%>"
									selected="<%=CommonUtil.nullToString(searchForm.getFormFieldToVal(count))%>" />
						</select>
					</td>
				<% } 
				
					if (fieldType.equals("INPUT_WITH_WILDCARD")) { %>
					<td class="col_value" colspan="2">
						<input type="text" name="field<%=count%>" value="<%=CommonUtil.nullToString(searchForm.getFormFieldVal(count))%>" size="40" class="t2" <% if(autoFillList != null && autoFillList.contains(String.valueOf(count))) { %>onblur="javascript:autofill(<%=count%>)" <% } %>>
						<input type="hidden" name="fieldTo<%=count%>" class="t2">
					</td>
				<%	} %>
			</tr>
		<% } %>
	<% 
	} %>

<% if(searchForm.isShowSearchBtn()){ %>
<tr valign="top">
	<td class="col_label"></td>
	<td class="col_value" colspan=2>
		<input type="button" value="Search" class="button1" onClick="javascript:doSubmit('search');">
		<input type="button" value="Reset" class="button1" onClick="javascript:formReset();">
	</td>
</tr>
<% } %>

</table>
<%
} 
%>



<br>

<% 	if(searchResultList!=null ){ 
		if(searchResultList.size()==500){%>
		<font color='red'>
		<bean:message key="message.search.warning" />		
		</font><br>
<%		}
 	}%>

<% if(searchForm.isShowOkCancelBtn()){ %>
<input type="button" value="  OK  " class="button1" onClick="javascript:doSubmit('ok');">
<input type="button" value="Cancel" class="button1" onClick="javascript:self.close();">
<% } %>

<% if (!searchForm.getIsExclusive()) { %>
<br><br>

<% if(searchForm.isShowSelectBox()){ %>
<input type="button" value="Select All" class="button1" onClick="javascript:doSubmit('selectAll');">
<input type="button" value="De-Select All" class="button1" onClick="javascript:doSubmit('deSelectAll');">
<% } %>

<% } %>
<br><br>
<table class="table_grid">
<tr align="center">

	<% if(searchForm.isShowSelectBox()){ %>
	<td class="grid_header" width=10>Select?</td>
	<% } %>
<%
ArrayList rsltTextList = (ArrayList) params.get(ProjectConstants.SESSION_RESULT_TEXT_LIST);
for (int i=0; rsltTextList!=null && i<rsltTextList.size(); i++) {
%>
	<td class="grid_header"><%=rsltTextList.get(i)%></td>
	<% totalDisplayRow--; %>
<% } %>
</tr>
<logic:iterate id="searchResultRow" name="searchResultList" indexId="idx"  >
<%
    SearchResultRow detail = (SearchResultRow) searchResultRow;
%>
<tr <%=(idx.intValue()%2 == 0)? "class=grid_result1": "class=grid_result2"%>> 

	<% if(searchForm.isShowSelectBox()){ %>
    <td width=10>
	    <input type="checkbox" name="isDelete" value="<%= detail.getIdx()%>" <%=detail.getIsDelete()%> onClick="javascript:doSelect(<%= idx %>);" />
    </td>
	<% } %>

    <% for (int i=1; rsltTextList!=null && i<=rsltTextList.size(); i++) { %>
		<td><%=CommonUtil.nullToString(PropertyUtils.getSimpleProperty(detail,"column"+i))%>&nbsp;</td>
    <% } %>
	<% totalDisplayRow--; %>
</tr>
<html:hidden property="idx" value="<%=detail.getIdx()%>" />
</logic:iterate>
</table>
<% if (totalDisplayRow <= 0) { %>
	<input type="button" value="  OK  " class="button1" onClick="javascript:doSubmit('ok');">
	<input type="button" value="Cancel" class="button1" onClick="javascript:self.close();">
<% } %>
</html:form>
</body>